﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
using MySql.Data.MySqlClient;

namespace MySQLLib
{
    public class Guild_storage
    {
        string[,] _guild_storage;
        private MySqlConnection conn;

        public void setConnection(MySqlConnection connection)
        {
            this.conn = connection;
        }

        public void refresh()
        {
            string[] column = new string[1];
            column[0] = "*";

            this._guild_storage = Query.SelectQuery(this.conn, "guild_storage", column);
        }

        public string[,] Guild_Storage
        {
            get { return this._guild_storage; }
        }

        public string[,] getItemsFromGuild(string guildname, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            string[,] items = new string[0, this._guild_storage.GetLength(1)];

            for (int i = 0; i < this._guild_storage.GetLength(0); i++)
            {
                if (this._guild_storage[i, 1] == guildname)
                {
                    Other.ResizetwoDimensArray(ref items, this._guild_storage.GetLength(1), this._guild_storage.GetLength(0) + 1);
                    for (int i2 = 0; i2 < this._guild_storage.GetLength(1); i2++)
                    {
                        items[items.GetLength(0) - 1, i2] = this._guild_storage[i, i2];
                    }
                }
            }

            return items;
        }

        public void deleteItem(int id)
        {
            Query.DeleteQuery(this.conn, "guild_storage", "id=" + id);
        }

        public void addItem(string guildname, int itemid, string itemnumber, int plus, int slot, int type, int quantity, int durability, int inavatar, int blueamount, int blue1, int blue1amount, int blue2, int blue2amount, int blue3, int blue3amount, int blue4, int blue4amount, int blue5, int blue5amount, int blue6, int blue6amount, int blue7, int blue7amount, int blue8, int blue8amount, int blue9, int blue9amount)
        {
            string[] columns = { "guildname", "itemid", "itemnumber", "plusvalue", "slot", "type", "quantity", "durability", "inavatar", "BlueAmount", "blue1", "blue1amount", "blue2", "blue2amount", "blue3", "blue3amount", "blue4", "blue4amount", "blue5", "blue5amount", "blue6", "blue6amount", "blue7", "blue7amount", "blue8", "blue8amount", "blue9", "blue9amount" };
            string[] values = { guildname, itemid.ToString(), itemnumber, plus.ToString(), slot.ToString(), type.ToString(), quantity.ToString(), durability.ToString(), inavatar.ToString(), blueamount.ToString(), blue1.ToString(), blue1amount.ToString(), blue2.ToString(), blue2amount.ToString(), blue3.ToString(), blue3amount.ToString(), blue4.ToString(), blue4amount.ToString(), blue5.ToString(), blue5amount.ToString(), blue6.ToString(), blue6amount.ToString(), blue7.ToString(), blue7amount.ToString(), blue8.ToString(), blue8amount.ToString(), blue9.ToString(), blue9amount.ToString() };
            Query.InsertQuery(this.conn, "guild_storage", columns, values);
        }

        public void setSlot(int id, int newSlot)
        {
            Query.UpdateQuery(this.conn, "guild_storage", "slot", newSlot.ToString(), "id=" + id);
        }

        public void updateAmount(int id, int newAmount)
        {
            Query.UpdateQuery(this.conn, "guild_storage", "quantity", newAmount.ToString(), "id=" + id);
        }

        public string[] getItem(int id, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            string[] item = new string[this._guild_storage.GetLength(1)];
            for (int i = 0; i < item.Length; i++)
            {
                item[i] = this._guild_storage[id - 1, i];
            }

            return item;
        }

        public void addBlue(int id, int bluecount, string blue, int blueamount)
        {
            Query.UpdateQuery(this.conn, "guild_storage", "BlueAmount", bluecount.ToString(), "id=" + id);
            Query.UpdateQuery(this.conn, "guild_storage", "blue" + bluecount.ToString() + ",blue" + bluecount.ToString() + "amount", bluecount + "," + blueamount.ToString(), "id=" + id);
        }

        public void deleteBlueAtIndex(int id, int bluetodelete)
        {
            List<List<string>> blues = new List<List<string>>();
            List<string> blue = new List<string>();
            int blueamount;
            string[] result = Query.SelectQuery(this.conn, "guild_storage", "BlueAmount", "id=" + id);
            Int32.TryParse(result[0], out blueamount);

            for (int i = 1; i < blueamount; i++)
            {
                result = Query.SelectQuery(this.conn, "guild_storage", "blue" + i + ",blue" + i + "amount", "id=" + id);
                blue.Add(result[0]);
                blue.Add(result[1]);
                blues.Add(blue);
                blue.Clear();
            }

            blues.RemoveAt(bluetodelete);

            string[] columns = { "blue" + (blues.Count + 1), "blue" + (blues.Count + 1) + "amount" };
            string[] values = { "0", "0" };

            Query.UpdateQuery(this.conn, "guild_storage", "BlueAmount", blues.Count.ToString(), "id=" + id);
            Query.UpdateQuery(this.conn, "guild_storage", columns, values, "id=" + id);

            for (int i = 1; i < blues.Count; i++)
            {
                Query.UpdateQuery(this.conn, "guild_storage", "blue" + i, blues[i][0], "id=" + id);
                Query.UpdateQuery(this.conn, "guild_storage", "blue" + i + "amount", blues[i][1], "id=" + id);
            }

        }
    }
}
